//
//  TrackingViewController.m
//  FinApp
//
//  Created by Victor Leandro Fernandez Rodrigo on 12/11/11.
//  Copyright (c) 2011 FIB-Fàcultat d'informàtica de Barcelona. All rights reserved.
//

#import "TrackingViewController.h"
#import "RestConsumer.h"
#import "AppDelegate.h"
#import "LocationManager.h"

@implementation TrackingViewController

@synthesize activateLabel, infoLabel, trackingSwitch, navBar;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [activateLabel setText:NSLocalizedString(@"_volsTracking", @"")];
    [infoLabel setText:NSLocalizedString(@"_trackingInfo", @"")];
    [navBar.topItem setTitle:NSLocalizedString(@"_alertes", @"")];
    UIBarButtonItem *okButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_ok", @"") style:UIBarButtonItemStyleDone target:self action:@selector(okPressed)];
    [navBar.topItem setRightBarButtonItem:okButton];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)viewWillDisappear:(BOOL)animated {
    [UIView beginAnimations:@"animation2" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration: 0.7];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO]; 
    [UIView commitAnimations]; }

- (void)dealloc {
    [activateLabel release];
    [infoLabel release];
    [trackingSwitch release];
    [navBar release];
    [super dealloc];
}

- (void) okPressed {
    [self.navigationController popViewControllerAnimated:YES];

    

}
     

- (IBAction)trackingSwitch:(id)sender {
    UISwitch *switchTr = (UISwitch*)sender;
    if ([switchTr isOn]) {
        [[RestConsumer sharedInstance] getTrackingNearLatitude:[[LocationManager sharedInstance] myLocation].coordinate.latitude
         
                                                  andLongitude:[[LocationManager sharedInstance] myLocation].coordinate.longitude 
                                                    withRadius:3000 
                                                  withDelegate:self];
        [(AppDelegate*)[[UIApplication sharedApplication] delegate] showBigActivityWithText:NSLocalizedString(@"_preparantTracking", @"")];
        [[MyCLController sharedInstance] startSignificantChangeUpdates];
    } else {
        [[LocationManager sharedInstance] stopLocation];
    }
    
}

#pragma mark - DataFetchingHandler

-(void)dataFetchingDidFailWithTag:(NSString*)tag andError:(NSError*)error {
    [(AppDelegate*)[[UIApplication sharedApplication] delegate] removeBigActivity];
}

-(void)dataFetchingCompleteWithTag:(NSString*)tag data:(id)aData andStatus:(NSInteger)status{
    NSDictionary *response = (NSDictionary*)aData;
    if ([[response objectForKey:@"responseCode"] intValue] != 200) {
        [self dataFetchingDidFailWithTag:tag andError:nil];
        return;
    }
    if ([tag isEqualToString:kTRACKINGTAG]) {
        //Get Data
        [(AppDelegate*)[[UIApplication sharedApplication] delegate] removeBigActivity];
        [[NSUserDefaults standardUserDefaults] setObject:[aData objectForKey:@"inmobles"] forKey:@"tracking"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

@end
